package com.jr.controller;

import com.jr.pojo.User;
import com.jr.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户注册控制器
 * 专门处理用户注册相关的请求
 */
@RestController
@RequestMapping("/api/register")
@CrossOrigin(origins = "*")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    /**
     * 用户注册接口
     * 支持三种用户类型：pet_owner（宠物主人）、care_provider（服务提供商）、platform_admin（平台管理员）
     */
    @PostMapping("/user")
    public Map<String, Object> registerUser(@RequestBody Map<String, Object> requestData) {
        try {
            // 提取用户基础信息
            User user = extractUserFromRequest(requestData);
            
            // 提取额外信息
            @SuppressWarnings("unchecked")
            Map<String, Object> additionalInfo = (Map<String, Object>) requestData.get("additionalInfo");
            
            // 调用注册服务
            return registrationService.registerUser(user, additionalInfo);
            
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "注册失败：" + e.getMessage());
            return result;
        }
    }

    /**
     * 验证注册数据
     */
    @PostMapping("/validate")
    public Map<String, Object> validateRegistration(@RequestBody User user) {
        return registrationService.validateRegistrationData(user);
    }

    /**
     * 检查用户名是否可用
     */
    @GetMapping("/check-username")
    public Map<String, Object> checkUsername(@RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 这里可以调用UserService检查用户名
            result.put("success", true);
            result.put("available", true); // 简化实现
            result.put("message", "用户名可用");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查用户名失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 检查邮箱是否可用
     */
    @GetMapping("/check-email")
    public Map<String, Object> checkEmail(@RequestParam String email) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 这里可以调用UserService检查邮箱
            result.put("success", true);
            result.put("available", true); // 简化实现
            result.put("message", "邮箱可用");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查邮箱失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 从请求数据中提取User对象
     */
    private User extractUserFromRequest(Map<String, Object> requestData) {
        User user = new User();
        
        user.setUsername((String) requestData.get("username"));
        user.setPassword((String) requestData.get("password"));
        user.setEmail((String) requestData.get("email"));
        user.setPhone((String) requestData.get("phone"));
        user.setUserType((String) requestData.get("userType"));
        user.setAvatarUrl((String) requestData.get("avatarUrl"));
        
        return user;
    }

    /**
     * 获取用户类型说明
     */
    @GetMapping("/user-types")
    public Map<String, Object> getUserTypes() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> userTypes = new HashMap<>();
        
        Map<String, String> petOwner = new HashMap<>();
        petOwner.put("value", "pet_owner");
        petOwner.put("label", "宠物主人");
        petOwner.put("description", "为宠物寻找寄养服务的用户");
        
        Map<String, String> careProvider = new HashMap<>();
        careProvider.put("value", "care_provider");
        careProvider.put("label", "服务提供商");
        careProvider.put("description", "提供宠物寄养服务的机构或个人");
        
        Map<String, String> platformAdmin = new HashMap<>();
        platformAdmin.put("value", "platform_admin");
        platformAdmin.put("label", "平台管理员");
        platformAdmin.put("description", "负责平台运营、管理和维护的人员");
        
        userTypes.put("pet_owner", petOwner);
        userTypes.put("care_provider", careProvider);
        userTypes.put("platform_admin", platformAdmin);
        
        result.put("success", true);
        result.put("userTypes", userTypes);
        
        return result;
    }
}
